package com.g4.shoppingsingle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g4.shoppingsingle.mapper.*;
import com.g4.shoppingsingle.pojo.*;
import com.g4.shoppingsingle.service.IDdOrderInfoService;
import com.g4.shoppingsingle.service.IDdUsersInfoService;
import com.g4.shoppingsingle.vo.AddOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-27
 */
@Slf4j
@Service
public class DdOrderInfoServiceImpl extends ServiceImpl<DdOrderInfoMapper, DdOrderInfo> implements IDdOrderInfoService {

    //用户表
    @Autowired
    private IDdUsersInfoService iDdUsersInfoService;

    //订单表
    @Autowired
    private DdOrderInfoMapper ddOrderInfoMapper;

    //订单明细表
    @Autowired
    private DdSubOrderInfoMapper ddSubOrderInfoMapper;

    //购物车表
    @Autowired(required = false)
    private DdCartDetailMapper ddCartDetailMapper;

    //商品配置信息表
    @Autowired(required = false)
    private DdKindTypeMapper ddKindTypeMapper;

    /**
     * 根据用户id获取订单
     * @param userId
     * @return
     */
    @Override
    public List<DdOrderInfo> getOrders(Integer userId) {

        //1、通过用户id去订单表查订单信息
        QueryWrapper<DdOrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);    // where user_id = 2;
        List<DdOrderInfo> orders=ddOrderInfoMapper.selectList(wrapper);
        return orders;
    }

    /**
     * 购物车添加订单
     * @param lis
     */
    @Override
    public String cartAddOrder(List<AddOrderVo> lis) {
        String orderName = "";
        String SPACE = " ";
        List<DdCartDetail> list = new ArrayList<>();
        BigDecimal total_price = new BigDecimal(0);
        //1、先去购物车表查询
        for (AddOrderVo vo : lis) {
            DdCartDetail po = ddCartDetailMapper.selectById(vo.getCartId());
            orderName += po.getPdName() +  SPACE + po.getPdVersion() + SPACE;
            DdKindType ddKindType = ddKindTypeMapper.selectById(po.getTypeId());//商品配置类型id
            BigDecimal price = ddKindType.getDiscountPrice();
            log.info("商品具体的金额为："+ price);
            total_price = total_price.add(price.multiply(new BigDecimal(po.getNum()))); //订单总金额
            list.add(po);
        }

        log.info("订单总金额为： " + total_price);
        //获取当前登录用户
        DdUsersInfo me = iDdUsersInfoService.getMe();
        //2、添加主订单
        DdOrderInfo info = new DdOrderInfo();
//        String orderName = UUID.randomUUID().toString();

        info.setOrderName(orderName);
        info.setAddressId(1);  //TODO 收货地址需要做
        info.setPayStatus(0); //0-未支付，1-已支付
        info.setSendStatus(0); //0-未发货 1-已发货
        info.setTotalPrice(total_price ); //订单总价
        info.setUserId(me.getId());  //用户id
        ddOrderInfoMapper.insert(info);

        //3、添加订单明细
        String orderId = info.getId();
        for (DdCartDetail cart : list){
            DdSubOrderInfo sub = new DdSubOrderInfo();
            DdKindType ddKindType = ddKindTypeMapper.selectById(cart.getTypeId());//商品配置类型id
            BigDecimal actualPrice = ddKindType.getDiscountPrice().multiply(new BigDecimal(cart.getNum()));//明细金额
            sub.setActualPrice(actualPrice);
            sub.setColor(cart.getColor());
            sub.setOrId(orderId);
            sub.setPdId(cart.getPdId());
            sub.setPdName(cart.getPdName());
            sub.setPdVersion(cart.getPdVersion());
            sub.setTypeId(cart.getTypeId());
            sub.setNum(cart.getNum());
            //添加订单明细表
            ddSubOrderInfoMapper.insert(sub);
        }
        return orderId;

    }

    /**
     * 立即选购添加订单
     * @param typeId
     * @param productId
     * @return
     */
    @Override
    public String addOrder(Integer typeId, Integer productId) {
        String space = " ";
        //1、查询商品相关信息
        DdKindType ddKindType = ddKindTypeMapper.selectById(typeId);
        String orderName = ddKindType.getPdName() + space + ddKindType.getPdVersion() + space;

        //获取当前登录用户
        DdUsersInfo me = iDdUsersInfoService.getMe();

        //2、插入订单表
        DdOrderInfo pojo = new DdOrderInfo();
        pojo.setOrderName(orderName);
        pojo.setPayStatus(0);
        pojo.setSendStatus(0);
        pojo.setTotalPrice(ddKindType.getPdPrice());
        pojo.setUserId(me.getId());
        pojo.setAddressId(1);  //TODO 收货地址
        ddOrderInfoMapper.insert(pojo);

        //3、插入订单明细表
        DdSubOrderInfo ddSubOrderInfo = new DdSubOrderInfo();
        ddSubOrderInfo.setActualPrice(ddKindType.getPdPrice());
        ddSubOrderInfo.setColor(ddKindType.getColor());
        ddSubOrderInfo.setNum(1);  //TODO 数量传过来
        ddSubOrderInfo.setOrId(pojo.getId());
        ddSubOrderInfo.setPdId(ddKindType.getPdId());
        ddSubOrderInfo.setPdName(ddKindType.getPdName());
        ddSubOrderInfo.setPdVersion(ddKindType.getPdVersion());
        ddSubOrderInfo.setTypeId(ddKindType.getId());
        ddSubOrderInfoMapper.insert(ddSubOrderInfo);
        //返回订单id
        return pojo.getId();
    }
}
